package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemSignManagerBinding;
import com.jinlejia.erp.utils.StringUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Map;

/**
 * 签到管理Adapter）
 */

public class SignManagerAdapter extends YfListAdapter<Map<String, Object>> {

    private Context context;
    private OnFieldOutClickListener listener;

    public SignManagerAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sign_manager, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ItemSignManagerBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final Map<String, Object> map = mData.get(position);
        binding.name.setText(map.get("userName").toString());
        //上班
        if (!map.containsKey("upperSignState")) {
            binding.signInState.setText("未签到");
        } else {
            switch (Integer.parseInt(StringUtil.valueOf(map.get("upperSignState"), "100"))) {
                case 0:
                    binding.signInState.setText("未签到");
                    break;
                case 1:
                    binding.signInState.setText("签到");
                    break;
                case 2:
                    binding.signInState.setText("迟到");
                    break;
                case 3:
                    binding.signInState.setText("早退");
                    break;
                default:
                    break;
            }
        }


        //是否外勤
        if (map.containsKey("upperSignType") && StringUtil.valueOf(map.get("upperSignType")).equals("2")) {
            binding.isFieldSignIn.setVisibility(View.VISIBLE);
        } else {
            binding.isFieldSignIn.setVisibility(View.GONE);
        }
        binding.isFieldSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != listener) {
                    listener.OnClick(map.get("pin").toString(), map.get("upperId").toString());
                }
            }
        });

        //下班

        if (!map.containsKey("lowerSignState")) {
            binding.signOutState.setText("未签退");
        } else {
            switch (Integer.parseInt(StringUtil.valueOf(map.get("lowerSignState"), "100"))) {
                case 0:
                    binding.signOutState.setText("未签退");
                    break;
                case 1:
                    binding.signOutState.setText("签退");
                    break;
                case 2:
                    binding.signOutState.setText("迟到");
                    break;
                case 3:
                    binding.signOutState.setText("早退");
                    break;
                default:
                    break;
            }
        }

        //是否外勤
        if (map.containsKey("lowerSignType") && StringUtil.valueOf(map.get("lowerSignType")).equals("2")) {
            binding.isFieldSignOut.setVisibility(View.VISIBLE);
        } else {
            binding.isFieldSignOut.setVisibility(View.GONE);
        }
        binding.isFieldSignOut.setOnClickListener(v -> {
            if (null != listener) {
                listener.OnClick(map.get("pin").toString(), map.get("lowerId").toString());
            }
        });

        binding.getRoot().setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }

    public interface OnFieldOutClickListener {
        void OnClick(String pin, String id);
    }

    public void setFieldOutClickListener(OnFieldOutClickListener listener) {
        this.listener = listener;
    }
}
